/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */

/*
 * AdminLogin.java
 *
 * Created on 19-10-2011, 11:15:49
 */
package swing;

import config.MyConnection;
import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Kenzou
 */
public class AdminLogin extends javax.swing.JFrame {

    Image icon = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/question_mark_icon_blueglow.png"));
    public static int x;
    public static int y;
    //Connection con = null;
    boolean check = false;
    ResultSet rs = null;
    //Statement st = null;
    ShowMessageDialog s;
    String user, pass;
    ImageIcon showmes = new ImageIcon("images/question_mark_icon_blueglow.png");
    ImageIcon showerror = new ImageIcon("images/dialog_warning.png");

    /** Creates new form AdminLogin */
    public AdminLogin() {
        initComponents();
        this.setAlwaysOnTop(true);
        config.Config.loadConfig();
        this.setIconImage(icon);
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbTitle = new javax.swing.JLabel();
        txtUserName = new javax.swing.JTextField();
        lbPassWord = new javax.swing.JLabel();
        lbUserName = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();
        btnReset = new javax.swing.JButton();
        btnLogin = new javax.swing.JButton();
        lbBottom = new javax.swing.JLabel();
        lbBackGround = new javax.swing.JLabel();
        menubar = new javax.swing.JMenuBar();
        mnSystem = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        mnExit = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        mnAbout = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        mnHelp = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("KBC Quiz Admin Cpanel");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setMinimumSize(new java.awt.Dimension(1024, 700));
        setName("AdminLogin"); // NOI18N
        setResizable(false);
        getContentPane().setLayout(null);

        lbTitle.setFont(new java.awt.Font("Verdana", 1, 30)); // NOI18N
        lbTitle.setForeground(new java.awt.Color(255, 255, 255));
        lbTitle.setText("Đăng nhập Admin");
        lbTitle.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        getContentPane().add(lbTitle);
        lbTitle.setBounds(360, 50, 400, 60);
        getContentPane().add(txtUserName);
        txtUserName.setBounds(480, 230, 150, 30);

        lbPassWord.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lbPassWord.setForeground(new java.awt.Color(255, 255, 255));
        lbPassWord.setText("PassWord");
        getContentPane().add(lbPassWord);
        lbPassWord.setBounds(350, 280, 100, 30);

        lbUserName.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lbUserName.setForeground(new java.awt.Color(255, 255, 255));
        lbUserName.setText("UserName");
        getContentPane().add(lbUserName);
        lbUserName.setBounds(350, 220, 100, 30);
        getContentPane().add(txtPassword);
        txtPassword.setBounds(480, 280, 150, 30);

        btnReset.setBackground(new java.awt.Color(255, 255, 255));
        btnReset.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btnReset1.png"))); // NOI18N
        btnReset.setContentAreaFilled(false);
        btnReset.setMaximumSize(new java.awt.Dimension(31, 15));
        btnReset.setMinimumSize(new java.awt.Dimension(31, 15));
        btnReset.setPreferredSize(new java.awt.Dimension(31, 15));
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });
        getContentPane().add(btnReset);
        btnReset.setBounds(560, 330, 70, 30);

        btnLogin.setBackground(new java.awt.Color(255, 255, 255));
        btnLogin.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnLogin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btnLogin.png"))); // NOI18N
        btnLogin.setBorder(null);
        btnLogin.setContentAreaFilled(false);
        btnLogin.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        btnLogin.setMaximumSize(new java.awt.Dimension(60, 15));
        btnLogin.setMinimumSize(new java.awt.Dimension(60, 15));
        btnLogin.setPreferredSize(new java.awt.Dimension(60, 15));
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });
        getContentPane().add(btnLogin);
        btnLogin.setBounds(470, 330, 70, 30);

        lbBottom.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbBottom.setForeground(new java.awt.Color(255, 255, 255));
        lbBottom.setText("Nhóm 6 - Lớp 55PM1 - Đại học Xây dựng");
        getContentPane().add(lbBottom);
        lbBottom.setBounds(390, 520, 270, 20);

        lbBackGround.setBackground(new java.awt.Color(57, 41, 125));
        lbBackGround.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/background_login.jpg"))); // NOI18N
        lbBackGround.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        getContentPane().add(lbBackGround);
        lbBackGround.setBounds(0, 0, 1020, 680);

        menubar.setBackground(javax.swing.UIManager.getDefaults().getColor("Menu.selectionBackground"));

        mnSystem.setText("System");

        jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/user.png"))); // NOI18N
        jMenuItem1.setText("Player");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        mnSystem.add(jMenuItem1);

        mnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit.png"))); // NOI18N
        mnExit.setText("Exit");
        mnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnExitActionPerformed(evt);
            }
        });
        mnSystem.add(mnExit);

        menubar.add(mnSystem);

        jMenu1.setText("About");

        mnAbout.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/about.png"))); // NOI18N
        mnAbout.setText("About");
        mnAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnAboutActionPerformed(evt);
            }
        });
        jMenu1.add(mnAbout);

        menubar.add(jMenu1);

        jMenu2.setText("Help");

        mnHelp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/help_copy.png"))); // NOI18N
        mnHelp.setText("Help");
        jMenu2.add(mnHelp);

        menubar.add(jMenu2);

        setJMenuBar(menubar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        txtUserName.setText("");
        txtPassword.setText("");
        txtUserName.setBackground(Color.white);
        txtPassword.setBackground(Color.white);
    }//GEN-LAST:event_btnResetActionPerformed

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        try {
            String strPass = new String(txtPassword.getPassword());
            if (txtUserName.getText().isEmpty()) {
                new ShowMessageDialog(null, true, "Please Enter UserName", "Confirmation", ShowMessageDialog.MESSAGE);
                txtUserName.setBackground(Color.yellow);
                txtUserName.requestFocus();
                return;
            }
            if (strPass.isEmpty()) {
                new ShowMessageDialog(null, true, "Please Enter PassWord", "Confirmation", ShowMessageDialog.MESSAGE);
                txtPassword.setBackground(Color.yellow);
                txtPassword.requestFocus();
                return;
            }

            Connection conn = MyConnection.getConn();
            if (conn == null) {
                new ShowMessageDialog(null, true, "You must config before loginning", "Confirmation", ShowMessageDialog.CONFIRM);
                int re = 0;
                if (re == JOptionPane.OK_OPTION) {
                    Config con = new Config(this, true);
                    con.setVisible(true);
                } else {
                    return;
                }
            } else {
                String sql = "select * from Admin";
                rs = MyConnection.resultSet(sql);

                while (rs.next()) {
                    user = rs.getString("UserName");
                    pass = rs.getString("Password");

                }
                if (txtUserName.getText().equals(user)) {
                    if (strPass.equals(pass)) {
                        ManagerAdmin mn = new ManagerAdmin();
                        mn.setVisible(true);
                        dispose();
                    } else {
                        new ShowMessageDialog(null, true, "Wrong PassWord  !!!!", "Confirmation", ShowMessageDialog.MESSAGE);
                        txtPassword.requestFocus();
                    }
                } else {
                    new ShowMessageDialog(null, true, "Wrong UserName !!!!", "Confirmation", ShowMessageDialog.MESSAGE);
                    txtUserName.requestFocus();
                }
            }
        } catch (SQLException ex) {
        }
    }//GEN-LAST:event_btnLoginActionPerformed

    private void mnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnExitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_mnExitActionPerformed

    private void mnAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnAboutActionPerformed
        About a = new About(null, true);
        a.setVisible(true);
    }//GEN-LAST:event_mnAboutActionPerformed

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
    new PlayerLogin().setVisible(true);
    this.dispose();
}//GEN-LAST:event_jMenuItem1ActionPerformed
    private void getConfig() {
        Config c = new Config(null, true);
        c.setVisible(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLogin;
    private javax.swing.JButton btnReset;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JLabel lbBackGround;
    private javax.swing.JLabel lbBottom;
    private javax.swing.JLabel lbPassWord;
    private javax.swing.JLabel lbTitle;
    private javax.swing.JLabel lbUserName;
    private javax.swing.JMenuBar menubar;
    private javax.swing.JMenuItem mnAbout;
    private javax.swing.JMenuItem mnExit;
    private javax.swing.JMenuItem mnHelp;
    private javax.swing.JMenu mnSystem;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables
}
